-
Notifications
You must be signed in to change notification settings - Fork 4k
sql: implement EXPLAIN (FINGERPRINT) statement #156152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
b7f9b41 to
7950a5f
Compare
|
After exploring it a bit, I don't think it's worth complicating things by implementing special behavior for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuzefovich reviewed 7 of 8 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status:complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball)
pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 166 at r2 (raw file):
query T EXPLAIN (FINGERPRINT) SELECT a, b, row_number() OVER (PARTITION BY b ORDER BY a) as rn
nit: let's use a window function that takes in some arguments and make some of those arguments constant.
pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 195 at r2 (raw file):
SELECT * FROM t WHERE a = _ # These two should have the same fingerprint (both should show "SELECT * FROM t WHERE a = _")
nit: seems like a leftover, or in the wrong spot.
pkg/sql/opt/optbuilder/explain.go line 34 at r2 (raw file):
// so don't. This allows someone to run EXPLAIN (FINGERPRINT) for statements // they don't have permission to execute, for example. Instead, we create a // dummy empty VALUES clause as input
nit: missing period.
pkg/sql/opt/optbuilder/explain.go line 88 at r2 (raw file):
} if explain.Mode == tree.ExplainFingerprint { stmtFingerprintFmtMask := tree.FmtHideConstants | tree.FmtFlags(tree.QueryFormattingForFingerprintsMask.Get(&b.evalCtx.Settings.SV))
nit: should we extract formatStatementHideConstants into tree package and reuse here?
Move `formatStatementHideConstants` and `formatStatementSummary` from sql to tree so that we can call `tree.FormatStatementHideConstants` from the optbuilder package. Epic: None Release note: None
This commit adds support for EXPLAIN (FINGERPRINT), a new EXPLAIN variant that returns statement fingerprints. Statement fingerprints are normalized forms of SQL statements where constants are replaced with underscores, making them useful for query pattern analysis and monitoring. Key features: - Returns a single row with single string column containing statement fingerprint - Respects sql.stats.statement_fingerprint.format_mask cluster setting Implementation details: - Added ExplainFingerprint mode to AST with validation - Implemented fingerprint computation during optbuild phase - Added telemetry tracking for usage - Comprehensive test coverage including edge cases and prepared statements Examples: - EXPLAIN (FINGERPRINT) SELECT * FROM t WHERE a = 123 Returns: "SELECT * FROM t WHERE a = _" Informs: cockroachdb#153633 Release note (sql change): Added EXPLAIN (FINGERPRINT) statement that returns normalized statement fingerprints with constants replaced by underscores. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
7950a5f to
c5ea6a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DrewKimball reviewed 14 of 14 files at r3, 8 of 8 files at r4, all commit messages.
Reviewable status:complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @michae2)
pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 218 at r4 (raw file):
# Test with cluster setting sql.stats.statement_fingerprint.format_mask set to 0 statement ok SET CLUSTER SETTING sql.stats.statement_fingerprint.format_mask = 0
Should we include a notice when the setting is non-default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TFTRs!
bors r=yuzefovich,DrewKimball
Reviewable status:
complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @DrewKimball)
pkg/sql/opt/optbuilder/explain.go line 34 at r2 (raw file):
Previously, yuzefovich (Yahor Yuzefovich) wrote…
nit: missing period.
Done.
pkg/sql/opt/optbuilder/explain.go line 88 at r2 (raw file):
Previously, yuzefovich (Yahor Yuzefovich) wrote…
nit: should we extract
formatStatementHideConstantsintotreepackage and reuse here?
Good point, done.
pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 166 at r2 (raw file):
Previously, yuzefovich (Yahor Yuzefovich) wrote…
nit: let's use a window function that takes in some arguments and make some of those arguments constant.
Done.
pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 195 at r2 (raw file):
Previously, yuzefovich (Yahor Yuzefovich) wrote…
nit: seems like a leftover, or in the wrong spot.
Done.
pkg/sql/opt/exec/execbuilder/testdata/explain_fingerprint line 218 at r4 (raw file):
Previously, DrewKimball (Drew Kimball) wrote…
Should we include a notice when the setting is non-default?
If this cluster setting is already set, then system.statement_statistics should also be using it. I don't think we need to print a notice, we just need to have the same behavior for system.statement_statistics, system.statement_hints, and EXPLAIN (FINGERPRINT).
sql, tree: move two formatting helper functions into tree
Move
formatStatementHideConstantsandformatStatementSummaryfrom sql to tree so that we can calltree.FormatStatementHideConstantsfrom the optbuilder package.Epic: None
Release note: None
sql: implement EXPLAIN (FINGERPRINT) statement
This commit adds support for EXPLAIN (FINGERPRINT), a new EXPLAIN variant that returns statement fingerprints. Statement fingerprints are normalized forms of SQL statements where constants are replaced with underscores, making them useful for query pattern analysis and monitoring.
Key features:
Implementation details:
Examples:
Informs: #153633
Release note (sql change): Added EXPLAIN (FINGERPRINT) statement that returns normalized statement fingerprints with constants replaced by underscores.
🤖 Generated with Claude Code